package com.iplant.basic.serviceImpl;

import java.io.File;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.cache.CacheUtils;
import com.iplant.base.utils.excel.MESFileUtils;
import com.iplant.base.utils.general.CalendarTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.orm.ORMUtils;
import com.iplant.base.utils.sql.orm.PoService;
import com.iplant.basic.service.basicenum.bfc.FMCShiftType;
import com.iplant.basic.service.po.sfc.SFCFile;
import com.iplant.basic.serviceImpl.dao.sfc.SFCFileDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.basic.service.FMCService;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.fmc.*;
import com.iplant.basic.serviceImpl.dao.fmc.*;

@Service
public class FMCServiceImpl implements FMCService {
    private static Logger logger = LoggerFactory.getLogger(BMSServiceImpl.class);

    public FMCServiceImpl() {
        // TODO Auto-generated constructor stub
    }

    private static FMCService Instance = null;

    public static FMCService getInstance() {
        if (Instance == null)
            Instance = new FMCServiceImpl();

        return Instance;
    }

    //region POService
    PoService<SFCFile> mSFCFileService = ORMUtils.GetPoService(SFCFile.class);
    //endregion


    @Override
    public ServiceResult<List<FMCShift>> FMC_QueryShiftList(BMSEmployee wLoginUser, int wFactoryID, int wWorkshopID,
                                                            int wWorkAreaID, int wStationID, Pagination wPagination) {

        ServiceResult<List<FMCShift>> wResult = new ServiceResult<List<FMCShift>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            wErrorCode.set(0);
            Map<String, Object> wParams = new HashMap<>();

            wParams.put("FactoryID", wFactoryID);
            wParams.put("WorkshopID", wWorkshopID);
            wParams.put("WorkAreaID", wWorkAreaID);
            wParams.put("StationID", wStationID);

            wResult.Result = FMCShiftDAO.getInstance().SelectPOList(wLoginUser, wParams, FMCShift.class, wPagination, wErrorCode);
            if (wErrorCode.Result != 0) {
                wResult.AddFaultCode(wErrorCode);
                return wResult;
            }
            Map<Integer,  FMCShift> wFMCShiftMap = new HashMap<>();

            if (wResult.Result != null && wResult.Result.size() > 0) {
                for (FMCShift wFMCShiftT: wResult.Result ) {
                    if(wFMCShiftMap.containsKey(wFMCShiftT.ShiftType))
                        continue;
                    wFMCShiftMap.put(wFMCShiftT.ShiftType,wFMCShiftT);
                }
                if (wFMCShiftMap.size()>=FMCShiftType.values().length-1){
                    wResult.Result=wFMCShiftMap.values().stream().sorted((o1,o2)->o1.ShiftType-o2.ShiftType).collect(Collectors.toList());
                    return wResult;
                }
            }
            //如果是工位，查工区
            if (wStationID > 0) {
                //查询单台工位

                FMCStation wFMCStation = CacheUtils.GetCacheInfo(FMCStation.class, wStationID, "");

                if (wFMCStation == null || wFMCStation.ID <= 0) {
                    wResult.FaultCode += StringUtils.Format("工位ID:{0}不存在！", wStationID);
                    return wResult;
                }
                wParams.clear();
                wFactoryID = wFMCStation.FactoryID;
                wWorkshopID = wFMCStation.WorkshopID;
                wWorkAreaID = wFMCStation.WorkAreaID;
                wParams.put("FactoryID", wFactoryID);
                wParams.put("WorkshopID", wWorkshopID);
                wParams.put("WorkAreaID", wWorkAreaID);
                wParams.put("StationID", 0);

                wResult.Result = FMCShiftDAO.getInstance().SelectPOList(wLoginUser, wParams, FMCShift.class, wPagination, wErrorCode);
                if (wErrorCode.Result != 0) {
                    wResult.AddFaultCode(wErrorCode);
                    return wResult;
                }
                if (wResult.Result != null && wResult.Result.size() > 0) {
                    for (FMCShift wFMCShift : wResult.Result) {
                        wFMCShift.ID = 0;
                        wFMCShift.StationID = wStationID;
                        if(wFMCShiftMap.containsKey(wFMCShift.ShiftType))
                            continue;
                        wFMCShiftMap.put(wFMCShift.ShiftType,wFMCShift);
                    }
                    if (wFMCShiftMap.size()>=FMCShiftType.values().length-1){
                        wResult.Result=wFMCShiftMap.values().stream().sorted((o1,o2)->o1.ShiftType-o2.ShiftType).collect(Collectors.toList());
                        return wResult;
                    }
                }
            }
            //如果是工位，查工区
            if (wWorkAreaID > 0) {
                //查询单台工位
                FMCWorkArea wFMCWorkArea = CacheUtils.GetCacheInfo(FMCWorkArea.class, wWorkAreaID, "");
                if (wErrorCode.Result != 0) {
                    wResult.AddFaultCode(wErrorCode);
                    return wResult;
                }
                if (wFMCWorkArea == null || wFMCWorkArea.ID <= 0) {
                    wResult.FaultCode += StringUtils.Format("工区ID:{0}不存在！", wWorkAreaID);
                    return wResult;
                }
                wParams.clear();
                wFactoryID = wFMCWorkArea.FactoryID;
                wWorkshopID = wFMCWorkArea.WorkshopID;
                wParams.put("FactoryID", wFactoryID);
                wParams.put("WorkshopID", wWorkshopID);
                wParams.put("WorkAreaID", 0);
                wParams.put("StationID", 0);

                wResult.Result = FMCShiftDAO.getInstance().SelectPOList(wLoginUser, wParams, FMCShift.class, wPagination, wErrorCode);
                if (wErrorCode.Result != 0) {
                    wResult.AddFaultCode(wErrorCode);
                    return wResult;
                }
                if (wResult.Result != null && wResult.Result.size() > 0) {
                    for (FMCShift wFMCShift : wResult.Result) {
                        wFMCShift.ID = 0;
                        wFMCShift.WorkAreaID = wWorkAreaID;
                        if (wStationID > 0) {
                            wFMCShift.StationID = wStationID;
                        }
                        if(wFMCShiftMap.containsKey(wFMCShift.ShiftType))
                            continue;
                        wFMCShiftMap.put(wFMCShift.ShiftType,wFMCShift);
                    }
                    if (wFMCShiftMap.size()>=FMCShiftType.values().length-1){
                        wResult.Result=wFMCShiftMap.values().stream().sorted((o1,o2)->o1.ShiftType-o2.ShiftType).collect(Collectors.toList());
                        return wResult;
                    }
                }

            }
            //如果是车间
            if (wWorkshopID > 0) {
                //查询车间
                FMCWorkshop wFMCWorkshop = CacheUtils.GetCacheInfo(FMCWorkshop.class, wWorkshopID, "");
                if (wErrorCode.Result != 0) {
                    wResult.AddFaultCode(wErrorCode);
                    return wResult;
                }
                if (wFMCWorkshop == null || wFMCWorkshop.ID <= 0) {
                    wResult.FaultCode += StringUtils.Format("车间ID:{0}不存在！", wWorkshopID);
                    return wResult;
                }
                wParams.clear();
                wFactoryID = wFMCWorkshop.FactoryID;
                wParams.put("FactoryID", wFactoryID);
                wParams.put("WorkshopID", 0);
                wParams.put("WorkAreaID", 0);
                wParams.put("StationID", 0);

                wResult.Result = FMCShiftDAO.getInstance().SelectPOList(wLoginUser, wParams, FMCShift.class, wPagination, wErrorCode);
                if (wErrorCode.Result != 0) {
                    wResult.AddFaultCode(wErrorCode);
                    return wResult;
                }
                if (wResult.Result != null && wResult.Result.size() > 0) {
                    for (FMCShift wFMCShift : wResult.Result) {
                        wFMCShift.ID = 0;
                        wFMCShift.WorkshopID = wWorkshopID;
                        if (wWorkAreaID > 0) {
                            wFMCShift.WorkAreaID = wWorkAreaID;
                        }
                        if (wStationID > 0) {
                            wFMCShift.StationID = wStationID;
                        }
                        if(wFMCShiftMap.containsKey(wFMCShift.ShiftType))
                            continue;
                        wFMCShiftMap.put(wFMCShift.ShiftType,wFMCShift);
                    }
                    if (wFMCShiftMap.size()>=FMCShiftType.values().length-1){
                        wResult.Result=wFMCShiftMap.values().stream().sorted((o1,o2)->o1.ShiftType-o2.ShiftType).collect(Collectors.toList());
                        return wResult;
                    }
                }
            }

            //如果是工厂
            if (wFactoryID > 0) {
                //查询工厂
                FMCFactory wFMCFactory = CacheUtils.GetCacheInfo(FMCFactory.class, wFactoryID, "");
                if (wErrorCode.Result != 0) {
                    wResult.AddFaultCode(wErrorCode);
                    return wResult;
                }
                if (wFMCFactory == null || wFMCFactory.ID <= 0) {
                    wResult.FaultCode += StringUtils.Format("工厂ID:{0}不存在！", wFactoryID);
                    return wResult;
                }
                //创建车间班次
                FMCShift wFMCShiftCreate;
                for (FMCShiftType wFMCShiftType : FMCShiftType.values()) {
                    if (wFMCShiftType.getValue() <= 0)
                        continue;
                    wFMCShiftCreate = new FMCShift();

                    wFMCShiftCreate.FactoryID = wFMCFactory.ID;
                    wFMCShiftCreate.ShiftType = wFMCShiftType.getValue();
                    wFMCShiftCreate.Minutes = 8 * 60;
                    wFMCShiftCreate.WorkMinutes = 8 * 60;

                    switch (wFMCShiftType) {

                        case Early:
                            wFMCShiftCreate.StartTime = CalendarTool.getCalendar(2020, 0, 1, 0, 0, 0);
                            wFMCShiftCreate.EndTime = CalendarTool.getCalendar(2020, 0, 1, 8, 0, 0);

                            break;
                        case Middle:
                            wFMCShiftCreate.StartTime = CalendarTool.getCalendar(2020, 0, 1, 8, 0, 0);
                            wFMCShiftCreate.EndTime = CalendarTool.getCalendar(2020, 0, 1, 16, 0, 0);
                            break;
                        case Night:
                            wFMCShiftCreate.StartTime = CalendarTool.getCalendar(2020, 0, 1, 16, 0, 0);
                            wFMCShiftCreate.EndTime = CalendarTool.getCalendar(2020, 0, 2, 0, 0, 0);
                            break;
                        case Default:
                        default:
                            continue;
                    }
                    FMCShiftDAO.getInstance().UpdatePo(wLoginUser, wFMCShiftCreate, wErrorCode);
                    if (wErrorCode.Result != 0) {
                        wResult.AddFaultCode(wErrorCode);
                        return wResult;
                    }
                }


                wParams.clear();
                wFactoryID = wFMCFactory.ID;
                wParams.put("FactoryID", wFactoryID);
                wParams.put("WorkshopID", 0);
                wParams.put("WorkAreaID", 0);
                wParams.put("StationID", 0);

                wResult.Result = FMCShiftDAO.getInstance().SelectPOList(wLoginUser, wParams, FMCShift.class, wPagination, wErrorCode);
                if (wErrorCode.Result != 0) {
                    wResult.AddFaultCode(wErrorCode);
                    return wResult;
                }
                if (wResult.Result != null && wResult.Result.size() > 0) {
                    for (FMCShift wFMCShift : wResult.Result) {
                        wFMCShift.ID = 0;
                        if (wWorkshopID > 0) {
                            wFMCShift.WorkshopID = wWorkshopID;
                        }
                        if (wWorkAreaID > 0) {
                            wFMCShift.WorkAreaID = wWorkAreaID;
                        }
                        if (wStationID > 0) {
                            wFMCShift.StationID = wStationID;
                        }
                        if(wFMCShiftMap.containsKey(wFMCShift.ShiftType))
                            continue;
                        wFMCShiftMap.put(wFMCShift.ShiftType,wFMCShift);
                    }
                    if (wFMCShiftMap.size()>=FMCShiftType.values().length-1){
                        wResult.Result=wFMCShiftMap.values().stream().sorted((o1,o2)->o1.ShiftType-o2.ShiftType).collect(Collectors.toList());
                        return wResult;
                    }
                } else {
                    wResult.FaultCode += StringUtils.Format("工厂ID:{0}创建班次数据失败！", wFactoryID);
                }
                return wResult;
            } else {
                wResult.FaultCode += RetCode.SERVER_RST_ERROR_OUT;
                return wResult;
            }
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> FMC_UpdateResource(BMSEmployee wLoginUser, FMCResource wResource) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            wErrorCode.set(0);
            FMCResourceDAO.getInstance().FMC_UpdateResource(wLoginUser, wResource, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> FMC_DeleteResource(BMSEmployee wLoginUser, FMCResource wResource) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            FMCResourceDAO.getInstance().DeletePO(wLoginUser, wResource, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> FMC_ActiveResource(BMSEmployee wLoginUser, FMCResource wResource, int wActive) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            FMCResourceDAO.getInstance().FMC_ActiveResource(wLoginUser, wResource, wActive, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<FMCResource> FMC_QueryResource(BMSEmployee wLoginUser, int wID) {
        ServiceResult<FMCResource> wResult = new ServiceResult<FMCResource>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            wErrorCode.set(0);
            wResult.Result = FMCResourceDAO.getInstance().FMC_QueryResource(wLoginUser, wID, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<FMCResource>> FMC_QueryResourceList(BMSEmployee wLoginUser, String wResourceCode,
                                                                  String wResourceName, List<Integer> wStationID, int wWorkType, int wTypeID, int wProductID, int wResourceID,
                                                                  int wActive, Pagination wPagination) {
        ServiceResult<List<FMCResource>> wResult = new ServiceResult<List<FMCResource>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            wErrorCode.set(0);
            wResult.Result = FMCResourceDAO.getInstance().FMC_QueryResourceList(wLoginUser, wResourceCode,
                    wResourceName, wStationID, wWorkType, wTypeID, wProductID, wResourceID, wActive, wPagination,
                    wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }


    @Override
    public ServiceResult<FMCStation> FMC_QueryStation(BMSEmployee wLoginUser, int wID, String wCode, int wHasResource) {
        ServiceResult<FMCStation> wResult = new ServiceResult<FMCStation>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            wErrorCode.set(0);
            wResult.Result = FMCStationDAO.getInstance().FMC_QueryStation(wLoginUser, wID, wCode, wHasResource,
                    wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<FMCStation>> FMC_QueryStationList(BMSEmployee wLoginUser, int wFactoryID, int wWorkshopID,
                                                                List<Integer> wLineID, int wWorkAreaID, int wDeviceID, String wName, int wActive, int wHasResource,
                                                                Pagination wPagination) {
        ServiceResult<List<FMCStation>> wResult = new ServiceResult<List<FMCStation>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            wErrorCode.set(0);
            wResult.Result = FMCStationDAO.getInstance().FMC_QueryStationList(wLoginUser, wFactoryID, wWorkshopID,
                    wLineID, wWorkAreaID, wDeviceID, wName, wActive, wHasResource, wPagination, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> FMC_UpdateStationFixtures(BMSEmployee wLoginUser,
                                                            FMCStationFixtures wStationFixtures) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            wErrorCode.set(0);
            FMCStationFixturesDAO.getInstance().FMC_UpdateStationFixtures(wLoginUser, wStationFixtures, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> FMC_DeleteStationFixtures(BMSEmployee wLoginUser,
                                                            FMCStationFixtures wStationFixtures) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            FMCStationFixturesDAO.getInstance().DeletePO(wLoginUser, wStationFixtures, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> FMC_ActiveStationFixtures(BMSEmployee wLoginUser, FMCStationFixtures wStationFixtures,
                                                            int wActive) {
        ServiceResult<Integer> wResult = new ServiceResult<Integer>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            FMCStationFixturesDAO.getInstance().FMC_ActiveStationFixtures(wLoginUser, wStationFixtures, wActive,
                    wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<FMCStationFixtures> FMC_QueryStationFixtures(BMSEmployee wLoginUser, int wID, String wCode) {
        ServiceResult<FMCStationFixtures> wResult = new ServiceResult<FMCStationFixtures>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            wErrorCode.set(0);
            wResult.Result = FMCStationFixturesDAO.getInstance().FMC_QueryStationFixtures(wLoginUser, wID, wCode,
                    wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<FMCStationFixtures>> FMC_QueryStationFixturesList(BMSEmployee wLoginUser, int wFactoryID,
                                                                                int wWorkshopID, int wLineID, int wWorkAreaID, List<Integer> wStationID, int wDeviceID, String wName,
                                                                                int wActive, Pagination wPagination) {
        ServiceResult<List<FMCStationFixtures>> wResult = new ServiceResult<List<FMCStationFixtures>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            wErrorCode.set(0);
            wResult.Result = FMCStationFixturesDAO.getInstance().FMC_QueryStationFixturesList(wLoginUser, wFactoryID,
                    wWorkshopID, wLineID, wWorkAreaID, wStationID, wDeviceID, wName, wActive, wPagination, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> SFC_UploadFile(BMSEmployee wLoginUser, SFCFile wSFCFile) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            //①根据网络路径获取实际物理路径
            ServiceResult<String> wOriFileSR = SFCFileDAO.getInstance().getFilePathByUrl(wLoginUser, wSFCFile.FileUrl
                    , wErrorCode);
            if (StringUtils.isNotEmpty(wOriFileSR.FaultCode)) {
                wResult.FaultCode += wOriFileSR.FaultCode;
                return wResult;
            }
            //②根据文件分类获取文件存储路径
            ServiceResult<String> wFileSavePathSR = SFCFileDAO.getInstance().getFileSavePathByClassification(wLoginUser,
                    wSFCFile.FileClassification, wErrorCode);
            if (StringUtils.isNotEmpty(wFileSavePathSR.FaultCode)) {
                wResult.FaultCode += wFileSavePathSR.FaultCode;
                return wResult;
            }
            //获取文件名称
            String wFileName = SFCFileDAO.getInstance().getFileNameByUrl(wLoginUser, wSFCFile.FileUrl, wErrorCode);
            if (StringUtils.isEmpty(wFileName)) {
                wResult.FaultCode += "解析文件名称失败!";
                return wResult;
            }
            //③文件复制
            String wFilePath = StringUtils.Format("{0}\\{1}", wFileSavePathSR.Result, wFileName);
            MESFileUtils.copyFile(wOriFileSR.Result, wFilePath);
            //④解析文件类型，如txt, docx, pdf
            String fileType = wFileName.substring(wFileName.lastIndexOf('.') + 1);
            //⑤获取文件大小
            long fileSize = MESFileUtils.getFileSize(wOriFileSR.Result);
            //⑥保存数据
            wSFCFile.Name = wFileName;
            wSFCFile.FilePath = wFilePath;
            wSFCFile.FileType = fileType;
            wSFCFile.FileSize = fileSize;
            wSFCFile.setUserInfo(wLoginUser);
            wSFCFile.EditTime = Calendar.getInstance();
            mSFCFileService.UpdatePo(wLoginUser, wSFCFile, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> SFC_DeleteFile(BMSEmployee wLoginUser, int wFileID) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            Map<String, Object> wParams = new HashMap<String, Object>();
            wParams.put("ID", wFileID);
            SFCFile sfcFile = mSFCFileService.SelectPO(wLoginUser, wParams,
                    wErrorCode);
            if (sfcFile == null || sfcFile.ID <= 0) {
                wResult.FaultCode += "未查询到文件信息!";
                return wResult;
            }

            mSFCFileService.DeletePO(wLoginUser, sfcFile, wErrorCode);
            if (StringUtils.isNotEmpty(sfcFile.FilePath)) {
                File wFile = new File(sfcFile.FilePath);
                if (wFile.exists()) {
                    wFile.delete();
                }
            }

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }
}
